/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author WKM
 * Date: Jul 5, 2010 9:48:13 AM
 */
package com.ccxe.manage.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccxe.manage.dao.impl.MenuManagerImpl;
import com.ccxe.manage.model.MenuBean;

/**
 * @author WKM
 * 
 */
public class MenuProcess {

	private MenuManagerImpl menuManagerImpl = new MenuManagerImpl();

	/**
	 * 通过ID来加载树
	 * 
	 * @param id
	 *            树节点ID
	 * @return 树根节点
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadMenuById(String id) {
		List<MenuBean> list = menuManagerImpl.getAllMenu();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getMenuId().equals(id)) {
				return makeTree(list.get(i), list, 0, 9999);
			}
		}
		return "";
	}

	/**
	 * 根据Id返回菜单的父亲节点,一直到根节点.
	 * 
	 * @param id
	 * @return
	 */
	public MenuBean getLinkMenuById(String id) {
		List<MenuBean> list = menuManagerImpl.getAllMenu();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getMenuId().equals(id)) {
				return searchTree(list, list.get(i));
			}
		}
		return new MenuBean();
	}

	private int getLevel(MenuBean bean){
		if(bean.getMenuId() != null 
				&& !bean.getMenuId().equals("")){
			if(bean.getList().size() <= 0){
				return 1;
			} else {
				return 1 +  getLevel(bean.getList().get(0));
			}
			
		}
		return 0;
	}
	
	/**
	 * 将返回父节点(一直到根节点),转换为HTML
	 * 
	 * @param root
	 * @return
	 */
	private String getLinkMenu(MenuBean root) {
		StringBuffer result = new StringBuffer();
		if (root.getMenuName() != null && !root.getMenuName().equals("")) {
			result.append(root.getMenuName());
		}

		if (root.getList().size() == 1) {
			result.append("&nbsp;");
			result.append("&gt;");
			result.append("&nbsp;");
			result.append(getLinkMenu(root.getList().get(0)));
		}

		return result.toString();
	}

	/**
	 * 搜寻父节点
	 * 
	 * @param list
	 * @param root
	 */
	private MenuBean searchTree(List<MenuBean> list, MenuBean root) {
		if (root.getParentId().equals("0")) {
			return root;
		}
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getMenuId().equals(root.getParentId())) {
				list.get(i).getList().add(root);
				return searchTree(list, list.get(i));
			}
		}
		return root;
	}

	/**
	 * 根据ID搜寻子树
	 * 
	 * @param id
	 *            树节点ID
	 * @return 树根节点
	 * @throws ServletException
	 * @throws IOException
	 */
	public List<MenuBean> getMenuById(String id) {
		List<MenuBean> list = menuManagerImpl.getAllMenu();
		List<MenuBean> resutlt = new ArrayList<MenuBean>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getMenuId().equals(id)) {
				dealTree(list.get(i), list, resutlt);
			}
		}
		return resutlt;
	}

	/**
	 * 根据ID搜索所有子节点
	 * 
	 * @param id
	 *            树节点ID
	 * @return 树根节点
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getSubMenu(String id,List<MenuBean> list,List<MenuBean> resutlt) {
		if(list == null || list.size() <= 0){
			list = menuManagerImpl.getAllMenu();
		}
		if(resutlt == null) {
			return;
		}
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getParentId().equals(id)) {
				resutlt.add(list.get(i));
				getSubMenu(list.get(i).getMenuId(),list, resutlt);
			}
		}
	}
	
	/**
	 * 搜寻子节点
	 * 
	 * @param root
	 * @param list
	 * @param result
	 */
	private void dealTree(MenuBean root, List<MenuBean> list,
			List<MenuBean> result) {
		for (int i = 0; i < list.size(); i++) {
			if (root.getMenuId().equals(list.get(i).getParentId())
					&& "1".equals(list.get(i).getStyleFlag())) {
				result.add(list.get(i));
				dealTree(list.get(i), list, result);
			}
		}
	}

	/**
	 * 构造树结构
	 * 
	 * @param root
	 *            根节点
	 * @param list
	 *            树列表
	 * @param level
	 *            级别
	 * @return 树结构
	 */
	public String makeTree(MenuBean root, List<MenuBean> list, int level,
			int depth) {
		StringBuffer rootBuffer = new StringBuffer();
		int currLevel = level;
		if (currLevel > depth)
			return "";
		rootBuffer.append("\r\n");
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getParentId().equals(root.getMenuId())
					&& "0".equals(list.get(i).getStyleFlag())) {
				// 首层展示
				if (list.get(i).getStyleFlag() == null
						|| list.get(i).getStyleFlag().equals("1")) {
					continue;
				}
				if (currLevel == 0) {
					boolean flag = true;

					if (list.get(i).getChildCount(list) <= 0) {
						flag = false;
					}

					rootBuffer
							.append("<a href=\"javascript:void(0);\" onclick=\"disLeftTab(this,");
					if (!flag) {
						rootBuffer.append("0");
					} else {
						rootBuffer.append(list.get(i).getChildCount(list));
					}
					rootBuffer
							.append("*20);activeParentMenu('rightIframe','servlet/menu?opType=4&menuId="
									+ list.get(i).getMenuId()
									+ "&level="
									+ currLevel + "');\"");
					rootBuffer.append("class=\"title\" title=\"" + list.get(i).getMenuName() + "\"><span>");
					rootBuffer.append(getSimpleName(list.get(i).getMenuName()));
					rootBuffer.append("</span></a>");
					rootBuffer.append("\r\n");

					if (flag) {
						if (currLevel == 0) {
							rootBuffer
									.append("<div class=\"sub\" style=\"display:none\">");
						} else {
							rootBuffer.append("<div class=\"sub" + currLevel
									+ "\" style=\"display:none\">");
						}
						rootBuffer.append(makeTree(list.get(i), list,
								level + 1, depth + 1));
						rootBuffer.append("</div>");
					}
					rootBuffer.append("\r\n");
					// 非首层的叶子节点
				} else if (currLevel != 0
						&& "1".equals(list.get(i).getIsLeaf())) {
					rootBuffer.append("\r\n");
					rootBuffer
							.append("<a href=\"javascript:void(0);\" onclick=\"javascript:leftMenuClick(this,'on','off');javascript:activeParentMenu('rightIframe','servlet/menu?opType=4&treeShow=true&menuId="
									+ list.get(i).getMenuId()
									+ "&level="
									+ currLevel + "');\" title=\"" + list.get(i).getMenuName() + "\">");
					rootBuffer.append(getSimpleName(list.get(i).getMenuName()));
					rootBuffer.append("</a>");
					rootBuffer.append("\r\n");
				} else if (currLevel != 0
						&& "0".equals(list.get(i).getIsLeaf())) {
					rootBuffer.append("\r\n");

					boolean flag = true;
					if (list.get(i).getChildCount(list) <= 0) {
						flag = false;
					}

					if (flag) {
						rootBuffer
								.append("<a href=\"javascript:void(0);\" onclick=\"javascript:disLeftTab"
										+ currLevel
										+ "(this,"
										+ list.get(i).getChildCount(list)
										+ "*20);javascript:activeParentMenu('rightIframe','servlet/menu?opType=4&treeShow=true&menuId="
										+ list.get(i).getMenuId()
										+ "&level="
										+ currLevel + "');\" title=\"" + list.get(i).getMenuName() + "\">");
						rootBuffer.append(getSimpleName(list.get(i).getMenuName()));
						rootBuffer.append("</a>");
						rootBuffer.append("\r\n");
						if (currLevel == 0) {
							rootBuffer
									.append("<div class=\"sub\" style=\"display:none\">");
						} else {
							rootBuffer.append("<div class=\"sub" + currLevel
									+ "\" style=\"display:none\">");
						}
						rootBuffer.append(makeTree(list.get(i), list,
								level + 1, depth + 1));
						rootBuffer.append("</div>");
					} else {
						rootBuffer
								.append("<a href=\"javascript:void(0);\" onclick=\"javascript:activeParentMenu('rightIframe','servlet/menu?opType=4&treeShow=true&menuId="
										+ list.get(i).getMenuId()
										+ "&level="
										+ currLevel + "');\" title=\"" + list.get(i).getMenuName() + "\">");
						rootBuffer.append(getSimpleName(list.get(i).getMenuName()));
						rootBuffer.append("</a>");
					}
					rootBuffer.append("\r\n");
				}
			}
		}
		rootBuffer.append("\r\n");
		return rootBuffer.toString();
	}

	/**
	 * 获取所有根节点
	 * 
	 * @return
	 */
	public List<MenuBean> getRootMenu() {
		return menuManagerImpl.getRootMenu();
	}

	//获取简称
	private String getSimpleName(String name){
		if(name.length() > 8){
			name = name.substring(0, 8) + "...";
		}
		return name;
	}
	
	/**
	 * 添加菜单项
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addMenu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		if (request.getParameter("addType") == null) {
			request.setAttribute("message", "添加类型错误!");
			makeTree(request, response);
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		}
		String addType = request.getParameter("addType");

		request.setAttribute("menuname", request.getParameter("menuname"));
		request.setAttribute("parentId", request.getParameter("parentId"));
		request.setAttribute("menuId", request.getParameter("menuId"));
		request.setAttribute("url", request.getParameter("url"));
		request.setAttribute("styleFlag", request.getParameter("styleFlag"));
		request.setAttribute("isLeaf", "1");
		MenuBean bean = new MenuBean();
		bean.setMenuName(request.getParameter("menuname"));
		bean.setMenuURL(request.getParameter("url"));//
		bean.setIsLeaf("1");
		bean.setStyleFlag(request.getParameter("styleFlag"));
		request.setAttribute("link", getLinkMenu(getLinkMenuById(request
				.getParameter("menuId"))));
		if (addType.equals("0")) {
			String parentId = request.getParameter("parentId");
			if (parentId == null || parentId.trim().equals("")) {
				bean.setParentId("0");
				request.setAttribute("parentId", "0");
				request.setAttribute("loadPage", "parent");
			} else {
				bean.setParentId(request.getParameter("parentId"));
				//搜寻父节点
				List<MenuBean> temp1 = menuManagerImpl.getMenuById(request
						.getParameter("menuId"));
				if(temp1.size() > 0){
					List<MenuBean> temp = menuManagerImpl.getMenuById(
							temp1.get(0).getParentId());
					if (temp.size() > 0) {
						if (temp.get(0).getStyleFlag().equals("1")) {
							bean.setStyleFlag("1");
						}
					}
				}
				if (request.getParameter("parentId").equals("0")) {
					request.setAttribute("loadPage", "parent");
				} else {
					request.setAttribute("loadPage", "left");
				}
				request.setAttribute("parentId", request
						.getParameter("parentId"));
			}
			if (bean.getMenuURL() == null
					|| bean.getMenuURL().trim().equals("")) {
				bean.setMenuURL("-");
			}
			menuManagerImpl.addMenuBean(bean);
			request.setAttribute("menuId", menuManagerImpl.getCurrId());

			makeTree(request, response);
			request.setAttribute("message", "添加成功");
			request.setAttribute("opdata", "add");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		} else if (addType.equals("1")) {
			String menuId = request.getParameter("menuId");
			if (menuId == null || menuId.trim().equals("")) {
				bean.setParentId("0");
				request.setAttribute("parentId", "0");
				request.setAttribute("loadPage", "parent");
			} else {
				//搜索父节点
				List<MenuBean> temp = menuManagerImpl.getMenuById(request
						.getParameter("menuId"));
				if (temp.size() > 0) {
					if (temp.get(0).getStyleFlag().equals("1")) {
						bean.setStyleFlag("1");
					}
				}
				bean.setParentId(request.getParameter("menuId"));
				request
						.setAttribute("parentId", request
								.getParameter("menuId"));
				request.setAttribute("loadPage", "left");
			}
			if (bean.getMenuURL() == null
					|| bean.getMenuURL().trim().equals("")) {
				bean.setMenuURL("-");
			}
			menuManagerImpl.addMenuBean(bean);
			request.setAttribute("menuId", menuManagerImpl.getCurrId());

			makeTree(request, response);
			request.setAttribute("message", "添加成功");
			request.setAttribute("opdata", "add");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		}
		makeTree(request, response);
		request.setAttribute("message", "添加类型错误");
		request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
				response);
	}

	private void makeTree(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 形成树
		String root = request.getParameter("root") == null ? "-9999" : request
				.getParameter("root");
		if ("".equals(root.trim())) {
			root = "-9999";
		}
		List<MenuBean> tree = getMenuById(root);
		List<MenuBean> rootBean = menuManagerImpl.getMenuById(root);
		if (tree.size() > 0) {
			request.setAttribute("treeShow", "true");
			for (int i = 0; i < tree.size(); i++) {
				if ("0".equals(tree.get(i).getStyleFlag())) {
					if (request.getAttribute("treeShow") != null) {
						request.removeAttribute("treeShow");
					}
				}
			}

		}
		if (rootBean.size() > 0) {
			request.setAttribute("rootId", root);
			request.setAttribute("rootName", rootBean.get(0).getMenuName());
		}
		request.setAttribute("root", request.getParameter("root"));
		request.setAttribute("treeList", tree);
		// 形成树END
	}

	/**
	 * 添加菜单项
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		MenuBean bean = new MenuBean();
		bean.setMenuName(request.getParameter("menuname"));
		bean.setMenuURL(request.getParameter("url"));//
		bean.setStyleFlag(request.getParameter("styleFlag"));
		bean.setParentId(request.getParameter("parentId"));
		bean.setMenuId(request.getParameter("menuId"));
		if (bean.getMenuURL() == null || bean.getMenuURL().trim().equals("")) {
			bean.setMenuURL("-");
		}
		
		// 形成树
		String root = request.getParameter("root") == null ? "" : request
				.getParameter("root");
		List<MenuBean> temp = getMenuById(root);
		List<MenuBean> rootBean = menuManagerImpl.getMenuById(root);
		if (temp.size() > 0) {
			request.setAttribute("treeShow", "true");
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).getStyleFlag().equals("0")) {
					if (request.getAttribute("treeShow") != null) {
						request.removeAttribute("treeShow");
					}
				}
			}

		}
		if (rootBean.size() > 0) {
			request.setAttribute("rootId", root);
			request.setAttribute("rootName", rootBean.get(0).getMenuName());
		}
		request.setAttribute("root", request.getParameter("root"));
		request.setAttribute("treeList", temp);
		// 形成树END

		// 菜单显示方式的修改
		List<MenuBean> menu = menuManagerImpl.getMenuById(bean.getMenuId());
		List<MenuBean> parent = menuManagerImpl.getMenuById(bean.getParentId());
		List<MenuBean> allMenu = menuManagerImpl.getAllMenu();
		boolean modifyFlag = true;
		if (menu.size() > 0) {
			if (!menu.get(0).getStyleFlag().equals(bean.getStyleFlag())) {
				if ("0".equals(bean.getStyleFlag())) {
					
						if (parent.size() > 0) {
							if ("1".equals(parent.get(0).getStyleFlag())) {
								modifyFlag = false;
							}
						} else {
							modifyFlag = false;
						}
				} else {
					
					for(int i = 0;i < allMenu.size();i++){
						if(allMenu.get(i).getParentId().equals(bean.getMenuId())
								&& "0".equals(allMenu.get(i).getStyleFlag())){
							modifyFlag = false;
						}
					}
						
				}
			}
		}
		
		//形成头链表
		MenuBean link = getLinkMenuById(request.getParameter("menuId"));
		int level = getLevel(link);
		if(level > 6 && 
				"0".equals(bean.getStyleFlag())){
			modifyFlag = false;
		}
		request.setAttribute("link", getLinkMenu(link));

		if (!modifyFlag) {
			request.setAttribute("menuId", menu.get(0).getMenuId());
			request.setAttribute("menuname", menu.get(0).getMenuName());
			request.setAttribute("url", menu.get(0).getMenuURL());
			request.setAttribute("styleFlag", menu.get(0).getStyleFlag());
			request.setAttribute("parentId", menu.get(0).getParentId());
			request.setAttribute("message", "<span style=\"color:#FF0000\">修改失败:无法处理的修改方式!</span>");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		}

		//更新数据
		menuManagerImpl.updateMenuBean(bean);
		
		// 数据更新完成,继续更新树并形成树
		root = request.getParameter("root") == null ? "" : request
				.getParameter("root");
		temp = getMenuById(root);
		rootBean = menuManagerImpl.getMenuById(root);
		if (temp.size() > 0) {
			request.setAttribute("treeShow", "true");
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).getStyleFlag().equals("0")) {
					if (request.getAttribute("treeShow") != null) {
						request.removeAttribute("treeShow");
					}
				}
			}

		}
		if (rootBean.size() > 0) {
			request.setAttribute("rootId", root);
			request.setAttribute("rootName", rootBean.get(0).getMenuName());
		}
		request.setAttribute("root", request.getParameter("root"));
		request.setAttribute("treeList", temp);
		// 形成树END
		
		List<MenuBean> list = menuManagerImpl.getMenuById(request
				.getParameter("menuId"));
		if (list.size() > 0) {
			request.setAttribute("menuId", list.get(0).getMenuId());
			request.setAttribute("menuname", list.get(0).getMenuName());
			request.setAttribute("url", list.get(0).getMenuURL());
			request.setAttribute("styleFlag", list.get(0).getStyleFlag());
			request.setAttribute("parentId", list.get(0).getParentId());
		}

		if (request.getParameter("parentId").equals("0")) {
			request.setAttribute("loadPage", "parent");
		} else {
			request.setAttribute("loadPage", "left");
		}

		request.setAttribute("message", "修改成功");
		request.setAttribute("opdata", "modify");
		request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
				response);
	}

	/**
	 * 删除菜单项
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteMenu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<MenuBean> loadBean = new ArrayList<MenuBean>();
		getSubMenu(request.getParameter("menuId"),null,loadBean);
		for (int i = 0; i < loadBean.size(); i++) {
			menuManagerImpl.deleteMenuBean(loadBean.get(i));
		}

		loadBean = menuManagerImpl.getMenuById(request.getParameter("menuId"));
		for (int i = 0; i < loadBean.size(); i++) {
			menuManagerImpl.deleteMenuBean(loadBean.get(i));
		}

		// 形成树
		String root = request.getParameter("root") == null ? "" : request
				.getParameter("root");
		List<MenuBean> temp = getMenuById(root);
		List<MenuBean> rootBean = menuManagerImpl.getMenuById(root);
		if (temp.size() > 0) {
			request.setAttribute("treeShow", "true");
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).getStyleFlag().equals("0")) {
					if (request.getAttribute("treeShow") != null) {
						request.removeAttribute("treeShow");
					}
				}
			}

		}
		if (rootBean.size() > 0) {
			request.setAttribute("rootId", root);
			request.setAttribute("rootName", rootBean.get(0).getMenuName());
		}
		request.setAttribute("root", request.getParameter("root"));
		request.setAttribute("treeList", temp);
		// 形成树END

		if (request.getParameter("parentId") == null) {
			request.setAttribute("message", "删除失败");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
		} else {
			if (request.getParameter("parentId").trim().equals("")
					|| request.getParameter("parentId").trim().equals("0")) {
				request.setAttribute("loadPage", "parent");
			} else {
				request.setAttribute("loadPage", "left");
			}
			request.setAttribute("menuId", request.getParameter("menuId"));
			request.setAttribute("message", "删除成功");
			request.setAttribute("opdata", "delete");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
		}
	}

	/**
	 * 展示菜单修改页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void showRight(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String menuId = request.getParameter("menuId");
		String target = request.getParameter("target");
		request.setAttribute("link", getLinkMenu(getLinkMenuById(menuId)));
		request.setAttribute("level",
				request.getParameter("level") == null ? "" : request
						.getParameter("level"));
		// 处理有菜单ID
		if (menuId == null) {
			request.setAttribute("message", "请求数据无效");
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		}

		// 只有菜单ID,非菜单的修改/新增页面
		if (target == null) {

			List<MenuBean> temp = getMenuById(menuId);
			List<MenuBean> currBean = menuManagerImpl.getMenuById(menuId);
			if (temp.size() > 0) {
				request.setAttribute("treeShow", "true");
				for (int i = 0; i < temp.size(); i++) {
					if (temp.get(i).getStyleFlag() == null) {
						continue;
					}
					if (temp.get(i).getStyleFlag().equals("0")) {
						if (request.getAttribute("treeShow") != null) {
							request.removeAttribute("treeShow");
						}
					}
				}

			}
			if (currBean.size() > 0) {
				request.setAttribute("menuname", currBean.get(0).getMenuName());
			}
			request.setAttribute("treeList", temp);

			List<MenuBean> list = menuManagerImpl.getMenuById(menuId);
			if (list.size() > 0) {
				request.setAttribute("menuId", list.get(0).getMenuId());
				request.setAttribute("menuname", list.get(0).getMenuName());
				request.setAttribute("url", list.get(0).getMenuURL());
				request.setAttribute("isLeaf", list.get(0).getIsLeaf());
				request.setAttribute("styleFlag", list.get(0).getStyleFlag());
				request.setAttribute("parentId", list.get(0).getParentId());
			}
			request.getRequestDispatcher("/jsp/menu/menu.jsp").forward(request,
					response);
			return;
		}
		String root = request.getParameter("root") == null ? "-9999" : request
				.getParameter("root");
		if ("".equals(root.trim())) {
			root = "-9999";
		}
		List<MenuBean> temp = getMenuById(root);
		List<MenuBean> rootBean = menuManagerImpl.getMenuById(root);
		if (temp.size() > 0) {
			request.setAttribute("treeShow", "true");
			for (int i = 0; i < temp.size(); i++) {
				if (temp.get(i).getStyleFlag().equals("0")) {
					if (request.getAttribute("treeShow") != null) {
						request.removeAttribute("treeShow");
					}
				}
			}

		}
		if (rootBean.size() > 0) {
			request.setAttribute("rootId", root);
			request.setAttribute("rootName", rootBean.get(0).getMenuName());
		}
		request.setAttribute("root", request.getParameter("root"));
		request.setAttribute("treeList", temp);

		request.setAttribute("menuId", request.getParameter("menuId"));
		request.setAttribute("parentId", request.getParameter("parentId"));
		// 页面新增
		if (target.equals("add")) {
			request.getRequestDispatcher("/jsp/menu/" + target + ".jsp")
					.forward(request, response);
			return;
		}

		// 页面的修改和删除
		List<MenuBean> list = menuManagerImpl.getMenuById(menuId);
		if (list.size() > 0) {
			request.setAttribute("menuId", list.get(0).getMenuId());
			request.setAttribute("menuname", list.get(0).getMenuName());
			request.setAttribute("url", list.get(0).getMenuURL());
			request.setAttribute("isLeaf", list.get(0).getIsLeaf());
			request.setAttribute("styleFlag", list.get(0).getStyleFlag());
			request.setAttribute("parentId", list.get(0).getParentId());
		}
		request.getRequestDispatcher("/jsp/menu/" + target + ".jsp").forward(
				request, response);
	}

	/**
	 * 显示左侧菜单
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void showLeft(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String menuId = request.getParameter("menuId");
		request.setAttribute("menuId", menuId);
		request.setAttribute("tree", loadMenuById(menuId));
		request.getRequestDispatcher("/jsp/menu/left.jsp").forward(request,
				response);
	}
}
